//
// Created by Zhangwj on 2023/9/26.
//

#include "StmtNode.h"
#include "StmtItemNode/BlockStmtItemNode.h"
#include "StmtItemNode/IfStmtItemNode.h"
#include "StmtItemNode/ForStmtItemNode.h"
#include "StmtItemNode/BreakStmtItemNode.h"
#include "StmtItemNode/ReturnStmtItemNode.h"
#include "StmtItemNode/PrintfStmtItemNode.h"
#include "StmtItemNode/LValExpStmtItemNode.h"
#include "StmtItemNode/ContinueStmtItemNode.h"

//Stmt → LVal '=' Exp ';'
//| [Exp] ';'
//| Block
//| 'if' '(' Cond ')' Stmt [ 'else' Stmt ]
//| 'for' '(' [ForStmt] ';' [Cond] ';' [forStmt] ')' Stmt
//| 'break' ';' | 'continue' ';'
//| 'return' [Exp] ';'
//| LVal '=' 'getint''('')'';'
//| 'printf''('FormatString{','Exp}')'';'

std::unique_ptr<StmtNode> StmtNode::StmtParser(Parser &parser) {
    std::unique_ptr<StmtNode> stmtNode = std::make_unique<StmtNode>();
    if (parser.getLexType(0) == LexType::LBRACE) {
        stmtNode->StmtItem = BlockStmtItemNode::BlockStmtItemParser(parser);
    } else if (parser.getLexType(0) == LexType::IFTK) {
        stmtNode->StmtItem = IfStmtItemNode::IfStmtItemParser(parser);
    } else if (parser.getLexType(0) == LexType::FORTK) {
        stmtNode->StmtItem = ForStmtItemNode::ForStmtItemParser(parser);
    } else if (parser.getLexType(0) == LexType::BREAKTK) {
        stmtNode->StmtItem = BreakStmtItemNode::BreakStmtItemParser(parser);
    } else if (parser.getLexType(0) == LexType::CONTINUETK) {
        stmtNode->StmtItem = ContinueStmtItemNode::ContinueStmtItemParser(parser);
    } else if (parser.getLexType(0) == LexType::RETURNTK) {
        stmtNode->StmtItem = ReturnStmtItemNode::ReturnStmtItemParser(parser);
    } else if (parser.getLexType(0) == LexType::PRINTFTK) {
        stmtNode->StmtItem = PrintfStmtItemNode::PrintfStmtItemParser(parser);
    } else {
        stmtNode->StmtItem = LValExpStmtItemNode::LValExpStmtItemParser(parser);
    }
    return stmtNode;
}
